package com.kaigejava.converter;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

import java.lang.reflect.Type;

/**
 * Created by kaigejava on 2019/8/4.
 * 消息转换类
 */
public class MyMessageConverter implements MessageConverter {
    @Override
    public Message toMessage(Object obj, MessageProperties messageProperties) throws MessageConversionException {
        return new Message(obj.toString().getBytes(),messageProperties);
    }

    @Override
    public Object fromMessage(Message message) throws MessageConversionException {
        String contenType = message.getMessageProperties().getContentType();
        if(!StringUtils.isEmpty(contenType)
                && contenType.contains("text")){
            return  new String (message.getBody());

        }
        return message.getBody();
    }

    @Override
    public Message toMessage(Object object, MessageProperties messageProperties, @Nullable Type genericType) throws MessageConversionException {
        return null;
    }


}
